package com.itheima.case2.utils;

import com.itheima.case2.pojo.po.UserLogin;
import com.itheima.case2.pojo.vo.Result;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
//配置拦截的资源路径
@WebFilter({"/user/*","/role/*","/permission/*"})
public class LoginFilter implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;
        UserLogin user =(UserLogin)request.getSession().getAttribute("user");
        if (user==null){
            BaseController.printResult(response,new Result(false,"暂未登录请先登陆！","-1"));
            //跳出该方法
            return;
        }else {
            chain.doFilter(request, response);
        }

    }

    public void init(FilterConfig config) throws ServletException {

    }

}